home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / rockridge / java / threads / classes / Clock.java < prev    next >
Encoding:
Java Source  |  1995-11-13  |  618 b   |  31 lines

  1.  
  2. import browser.Applet;
  3. import awt.Graphics;
  4. import java.util.Date;
  5.  
  6. class Clock extends Applet implements Runnable {
  7.  
  8.     Thread clockThread;
  9.  
  10.     public void start() {
  11.     if (clockThread == null) {
  12.         clockThread = new Thread(this, "Clock");
  13.         clockThread.start();
  14.     }
  15.     }
  16.     public void run() {
  17.     while (clockThread != null) {
  18.         repaint();
  19.         clockThread.sleep(1000);
  20.     }
  21.     }
  22.     public void paint(Graphics g) {
  23.     Date now = new Date();
  24.     g.drawString(now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds(), 5, 10);
  25.     }
  26.     public void stop() {
  27.     clockThread.stop();
  28.     clockThread = null;
  29.     }
  30. }
  31.